#ifndef __APP_LIB_POSIX_MSG_HPP__
#define __APP_LIB_POSIX_MSG_HPP__

#ifdef __cplusplus
extern "C" {
#endif

#include <mqueue.h>

#define APP_LIB_POSIX_MQ_INIT(x, mqName, max, size) \
do { \
    strncpy(x.szMqName, mqName, sizeof(x.szMqName) - 1); \
    x.maxmsg = max; \
    x.msgsize = size; \
} while (0);

typedef struct __APP_LIB_POSIX_MQ_s__
{
    mqd_t mqId;
    CHAR szMqName[256];
    SINT32 maxmsg;
    SINT32 msgsize;
} APP_LIB_POSIX_MQ_s;

typedef struct __APP_LIB_POSIX_MQ_MSG_s__
{
    UINT32 prio;
    SINT32 len;
    CHAR   data[0]; 
} APP_LIB_POSIX_MQ_MSG_s;

/*
    mkdir /dev/mqueue
    mount -t mqueue none /dev/mqueue

    [root@localhost ~]# cat  /proc/sys/fs/mqueue/msg_max
    10
    [root@localhost ~]# cat  /proc/sys/fs/mqueue/msgsize_max
    8192
    [root@localhost ~]# cat  /proc/sys/fs/mqueue/queues_max
    256
*/
extern ULONG AppLibPosix_MqOpen(INOUT APP_LIB_POSIX_MQ_s *pstMq);
extern ULONG AppLibPosix_MqClose(INOUT APP_LIB_POSIX_MQ_s *pstMq);
extern ULONG AppLibPosix_MqDestory(INOUT APP_LIB_POSIX_MQ_s *pstMq);
/*
    msg_prio：消息的优先级；它是一个小于MQ_PRIO_MAX的数，数值越大，优先级越高。
              POSIX消息队列在调用mq_receive时总是返回队列中最高优先级的最早消息。
              如果消息不需要设定优先级，那么可以在mq_send是置msg_prio为0，mq_receive的msg_prio置为NULL。
*/
extern ULONG AppLibPosix_MqSend(IN APP_LIB_POSIX_MQ_s *pstMq, IN APP_LIB_POSIX_MQ_MSG_s *pstMsg, IN struct timespec *timeout);
extern ULONG AppLibPosix_MqSend(IN APP_LIB_POSIX_MQ_s *pstMq, IN APP_LIB_POSIX_MQ_MSG_s *pstMsg, IN struct timespec *timeout);
extern ULONG AppLibPosix_MqSend(IN APP_LIB_POSIX_MQ_s *pstMq, IN APP_LIB_POSIX_MQ_MSG_s *pstMsg, IN struct timespec *timeout);
extern ULONG AppLibPosix_MqRecv(IN APP_LIB_POSIX_MQ_s *pstMq, INOUT APP_LIB_POSIX_MQ_MSG_s *pstMsg, IN struct timespec *timeout);

#ifdef __cplusplus
}
#endif

#endif
